﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NabNetLib.Packet
{
    public enum PacketType
    {
        Ambient = 0x04, Message = 0x0A, Sleep = 0x0B
    }

    public abstract class Packet
    {
        public abstract PacketType GetPacketType();

        protected abstract byte[] GetInternalData();


        public byte[] GetData()
        {
            List<byte> tmp = new List<byte>();
            byte[] data = this.GetInternalData();
            tmp.Add(0x7f);
            tmp.Add((byte)GetPacketType());
            byte len = (byte) data.Length; // Header(1) + Type(1) + Len(3) + Trail(1)
            tmp.Add((byte) (len >> 16));
            tmp.Add((byte) (len >> 8));
            tmp.Add(len);
            tmp.AddRange(data);
            tmp.Add(0xFF);
            return tmp.ToArray();
        }

        public static byte[] GetData(List<Packet> list)
        {
            List<byte> tmp = new List<byte>();
            tmp.Add(0x7f);
            foreach (Packet p in list)
            {
                byte[] data = p.GetInternalData();
                tmp.Add((byte)p.GetPacketType());
                byte len = (byte)data.Length; // Header(1) + Type(1) + Len(3) + Trail(1)
                tmp.Add((byte)(len >> 16));
                tmp.Add((byte)(len >> 8));
                tmp.Add(len);
                tmp.AddRange(data);
            }
            tmp.Add(0xFF);
            return tmp.ToArray();
        }
    }
}
